package com.zking.web.lj;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.zking.dao.domain.Information;
import com.zking.service.lj.InfService;
import com.zking.utils.RedisUtil;

@Controller
public class InfController {
	@Autowired
	InfService its;

	@Autowired
	private RedisUtil redisUtil;

	//查询所有公司
	@RequestMapping("lsetinfobj")
	public String lsetinfobj() {
		List<Information> lists = its.getInfs();
		if (redisUtil.lGetListSize("informationss")==0) {
			for (Information information : lists) {
				redisUtil.lSet("informationss", JSONObject.toJSONString(information),30);
			}
			return "redirect:/lgetinfobj";
		}else {
			return "redirect:/lgetinfobj";
		}
	}
	//使用Redis分页查询所有公司
	@RequestMapping("/lgetinfobj")
	public String lgetinfobj(HttpServletRequest request) {
		String pnS = request.getParameter("pn");

		int pn = 0;
		int limit = 5;
		try{
			pn = Integer.parseInt(pnS);
		} catch (Exception e) {
			pn = 1;
		}

		if(pn < 1){
			pn = 1;
		}

		int total = (int)redisUtil.lGetListSize("informationss");
		int pages = total / limit;
		if(total % limit != 0){
			pages++;
		}

		if(pn > pages){
			pn = pages;
		}

		int start = (pn - 1)*limit;//第一个参数
		limit =start+5; 
		List<Object> information= redisUtil.lGet("informationss", start, limit-1);
		List<Information> lists = new ArrayList<Information>();
		for (Object object : information) {
			lists.add(JSONObject.parseObject(object.toString(), Information.class));
		}
		if (!lists.isEmpty()) {
			request.setAttribute("lists", lists);
			request.setAttribute("pn", pn);
			request.setAttribute("limit", limit);
			request.setAttribute("pages", pages);
			return "/WEB-INF/admin/topiclist";
		}else {
			request.setAttribute("msg", "没有查询到数据");
			return "/WEB-INF/admin/topiclist";
		}
	}
	//查询未审核的公司
	@RequestMapping("/getInformations")
	public String getInformations(String com_audit,HttpServletRequest request) {
		List<Information> lists = its.getInformations(com_audit);
		if (redisUtil.lGetListSize("informations")==0) {
			for (Information list : lists) {
				redisUtil.lSet("informations", JSONObject.toJSONString(list));
			}
			return "redirect:/gethmget";
		}else {
			return "redirect:/gethmget";
		}
	}
	//使用Redis分页查询
	@RequestMapping("/gethmget")
	public String gethmget(HttpServletRequest request) {
		String gethmget = "gethmget";
		String pnS = request.getParameter("pn");

		int pn = 0;
		int limit = 5;
		try{
			pn = Integer.parseInt(pnS);
		} catch (Exception e) {
			pn = 1;
		}

		if(pn < 1){
			pn = 1;
		}

		int total = (int)redisUtil.lGetListSize("informations");
		int pages = total / limit;
		if(total % limit != 0){
			pages++;
		}

		if(pn > pages){
			pn = pages;
		}

		int start = (pn - 1)*limit;//第一个参数
		limit =start+5; 
		List<Object> information= redisUtil.lGet("informations", start, limit-1);
		List<Information> lists = new ArrayList<Information>();
		for (Object object : information) {
			lists.add(JSONObject.parseObject(object.toString(), Information.class));
		}
		if (!lists.isEmpty()) {
			request.setAttribute("lists", lists);
			request.setAttribute("site", gethmget);
			request.setAttribute("pn", pn);
			request.setAttribute("limit", limit);
			request.setAttribute("pages", pages);
			return "/WEB-INF/admin/topic";
		}else {
			request.setAttribute("msg", "没有查询到数据");
			return "/WEB-INF/admin/topic";
		}
	}

	//删除redis中的数据
	@RequestMapping("delinfobj")
	public String delinfobj() {
		String com_audit = null;
		try {
			com_audit = URLEncoder.encode("未审核", "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		redisUtil.del("informations");
		return "redirect:/getInformations?com_audit="+com_audit;
	}

	//根据id查询一条数据
	@RequestMapping("/getInformation")
	public String getInformation(Integer id,HttpServletRequest request) {
		Information information = its.getInformation(id); 
		if(information != null) {
			request.setAttribute("information", information);
			return "/WEB-INF/admin/topicupdate";
		}
		return "";
	}
	//修改公司审核状态
	@RequestMapping("/updateInformation")
	public String updateInformation(Integer id, String com_audit,String com_weibo,
			String reply,String to, String subject, String content) {
		subject = "面试审核结果";
		if ("已通过".equals(com_audit)) {
			int i = its.updateInformation(id, com_audit,com_weibo,subject,reply);
			if (i==1) {
				return "redirect:/delinfobj";
			}else {
				return "redirect:/delinfobj";
			}
		}else {
			int i = its.deleteinf(id);
			if (i==1) {
				return "redirect:/sendMail?to="+com_weibo+"&content="+reply;
			}
			return "redirect:/delinfobj";
		}
	}
	    //查询未审核、已通过、未通过的公司
		@RequestMapping("/getInfcau")
		public String getInfcau(String com_audit,HttpServletRequest request) {
			List<Information> lists = its.getInformations(com_audit);
			if (redisUtil.lGetListSize("getInfcau")==0) {
				for (Information list : lists) {
					redisUtil.lSet("getInfcau", JSONObject.toJSONString(list),300);
				}
				return "redirect:/lgetInfcau";
			}else {
				return "redirect:/lgetInfcau";
			}
		}
		//使用Redis分页查询
		@RequestMapping("/lgetInfcau")
		public String lgetInfcau(HttpServletRequest request) {
			String lgetInfcau = "lgetInfcau";
			String pnS = request.getParameter("pn");

			int pn = 0;
			int limit = 5;
			try{
				pn = Integer.parseInt(pnS);
			} catch (Exception e) {
				pn = 1;
			}

			if(pn < 1){
				pn = 1;
			}

			int total = (int)redisUtil.lGetListSize("getInfcau");
			int pages = total / limit;
			if(total % limit != 0){
				pages++;
			}

			if(pn > pages){
				pn = pages;
			}

			int start = (pn - 1)*limit;//第一个参数
			limit =start+5; 
			List<Object> information= redisUtil.lGet("getInfcau", start, limit-1);
			List<Information> lists = new ArrayList<Information>();
			for (Object object : information) {
				lists.add(JSONObject.parseObject(object.toString(), Information.class));
			}
			if (!lists.isEmpty()) {
				request.setAttribute("lists", lists);
				request.setAttribute("pn", pn);
				request.setAttribute("site", lgetInfcau);
				request.setAttribute("limit", limit);
				request.setAttribute("pages", pages);
				return "/WEB-INF/admin/topiclist";
			}else {
				request.setAttribute("msg", "没有查询到数据");
				return "/WEB-INF/admin/topiclist";
			}
		}
		//删除redis中的数据
		@RequestMapping("delInfcau")
		public String delInfcau(String com_audit) {
			try {
		    com_audit = URLEncoder.encode(com_audit, "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			redisUtil.del("getInfcau");
			return "redirect:/getInfcau?com_audit="+com_audit;
			
		}
	/*
	 * map
	 */
	//	//查询未审核的公司
	//		@RequestMapping("/getInformations")
	//		public String getInformations(String com_audit,HttpServletRequest request) {
	//			if (redisUtil.hmget("informations").isEmpty()) {
	//				List<Information> lists = its.getInformations(com_audit);
	//				Map<String, Object> map = new HashMap<String, Object>();
	//				int i = 1;
	//				for (Information information : lists) {
	//					System.out.println(information.toString());
	//					map.put("information"+i, JSONObject.toJSONString(information));
	//					redisUtil.hmset("informations", map);
	//					i++;
	//				}
	//				return "redirect:/gethmget";
	//			}else {
	//				return "redirect:/gethmget";
	//			}
	//		}
	//		//使用Redis分页查询
	//			@RequestMapping("/gethmget")
	//			public String gethmget(HttpServletRequest request) {
	//				Map<Object, Object> map =  redisUtil.hmget("informations");
	//				System.out.println(redisUtil.hmget("informations").isEmpty());
	//				List<Information> lists = new ArrayList<Information>();
	//				for(Object s:map.keySet()){
	//					String redisUserStr = (String) map.get(s);
	//					lists.add(JSONObject.parseObject(redisUserStr, Information.class));
	//		     }
	//				request.setAttribute("lists", lists);
	//				return "/WEB-INF/admin/topic";
	//			}

}
